<script setup lang='ts'>
import { ref, watch } from 'vue';
import { messageBox } from "earthsdk-ui";
type Number2s = [number, number][]
const props = withDefaults(defineProps<{
    modelValue?: Number2s,
    defaultValue?: Number2s,
    withUndefined?: boolean
    readonly?: boolean,
    propertyName?: string
}>(), { withUndefined: true, propertyName: '属性值', readonly: false });

const emits = defineEmits(['update:modelValue']);
const number2sVueRef = ref<Number2s>([])
const timers = ref<NodeJS.Timeout>()

const inputHandler = (e: any, index: number, num: number) => {
    clearTimeout(timers.value)
    timers.value = setTimeout(() => {
        const number2Value = [...(JSON.parse(JSON.stringify(number2sVueRef.value)))] as Number2s
        number2Value[index][num] = Number(e.target.value)
        number2sVueRef.value = number2Value
        ok()
    }, 200);
}

const addNumber = () => {
    number2sVueRef.value.push([0, 0])
    ok()
}

const toNumber2s = () => {
    if (props.modelValue === undefined) {
        const arr = JSON.parse(JSON.stringify(props.defaultValue ?? [])) as Number2s
        number2sVueRef.value = [...arr]
    } else {
        const arr = JSON.parse(JSON.stringify(props.modelValue)) as Number2s
        number2sVueRef.value = [...arr]
    }
}
watch(()=>props.modelValue,()=>{
    toNumber2s()
},{immediate:true})
const ok = () => {
    const arr = JSON.parse(JSON.stringify(number2sVueRef.value)) as Number2s
    emits('update:modelValue', [...arr])
}
const deleteIndex = (index: number) => {
    messageBox({ text: '确认删除这个位置点' })
        .then(() => {
            number2sVueRef.value.splice(index, 1)
            ok()
        })
        .catch((err) => {
        })
}
const hoverIndex = ref(-2)

</script>

<template>
    <div class="prop_list" :class="{ 'readonly_style': readonly }">
        <div class="points_top" style="margin: 0 0 5px 0;">
            <span>坐标</span>
            <span @click.stop="addNumber()" title="添加点" @mouseenter="hoverIndex = -1"
                @mouseleave="hoverIndex = -2"><es-icon :name="'tianjia'" :color="hoverIndex === -1 ? '#fff' : '#575B66'"
                    :size="13" /></span>
        </div>
        <div class="list_top" style="margin: 0 0 5px 0;">
            <span>序号</span>
            <span style="margin-left: 51px;"> x</span>
            <span style="margin-left: 86px;">y</span>
        </div>
        <div class="value_box" v-for="(item, index) in number2sVueRef" :key="index">
            <span class="zuobiao_btn">{{ index + 1 }}</span>
            <input type="number" class="input_number" :value="item[0]" @input="inputHandler($event, index, 0)" :disabled="readonly" />
            <input type="number" class="input_number" :value="item[1]" @input="inputHandler($event, index, 1)" :disabled="readonly" />
            <div class="content_right" @click="deleteIndex(index)" title="删除点" @mouseenter="hoverIndex = index"
                @mouseleave="hoverIndex = -2">
                <span class="td_span">
                    <es-icon :name="'shanchu_2'" :color="hoverIndex === index ? '#fff' : '#575B66'" :size="13" />
                </span>
            </div>
        </div>
    </div>

</template>

<style scoped>
.prop_list {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    border-bottom: 1px solid #111111;
}

.points_top {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    color: rgba(230, 230, 230, 1);
    cursor: pointer;
    margin-bottom: 5px;
}

.list_top {
    width: 100%;
    height: 22px;
    background-color: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    padding-left: 15px;
    line-height: 22px;
    box-sizing: border-box;
    color: rgba(230, 230, 230, 1);

}

.content_right {
    width: 30px;
    cursor: pointer;
}

.td_span {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;

}

.value_box {
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin: 5px 0px;
    align-items: center;
    color: rgba(230, 230, 230, 1);
}

.value_box>input {
    width: 34%;
    height: 20px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
    box-sizing: border-box;
}
input:hover {
    border: 1px solid #2c68f7;
}

input:focus {
    border: 1px solid #2c68f7;
}



.zuobiao_btn {
    width: 37px;
    height: 20px;
    font-size: 14px;
    line-height: 22px;
    margin-left: 10px;
    border-radius: 3px;
    text-align: center;
    cursor: pointer;
}

.readonly_style {
    cursor: not-allowed;
    cursor: no-drop;
    user-select: none;
}
</style>
